on clickMenuItem(theString)
	-- based on `menu_click`, by Jacob Rus, September 2006
	-- http://hints.macworld.com/article.php?story=20060921045743404
	-- example: clickMenuItem("Safari, Fichier, Nouvel onglet")
	set AppleScript's text item delimiters to ", "
	set theList to every text item of theString
	if theList's length < 3 then error "Menu list '" & theString & "' is not long enough"
	set {appName, topMenu} to (items 1 through 2 of theList)
	tell app appName to activate
	set tail to (items 3 through end of theList)
	script internal
		on recurs(theList, parentObject)
			set head to item 1 of theList
			if theList's length is 1 then
				tell app "System Events" to click parentObject's menu item head
			else
				my recurs((items 2 through end of theList), (parentObject's (menu item head)'s (menu head)))
			end if
		end recurs
	end script
	tell app "System Events"
		internal's recurs(tail, ((process appName)'s (menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
		keystroke tab using command down
	end tell
end clickMenuItem

on run
	set stdin to do shell script "cat" 
	clickMenuItem(stdin)
end run
